/**
 * Copyright &copy; 2012-2016 Civet All rights reserved.
 */
package com.civet.myadmin.modules.enviroment.envController;

import com.civet.myadmin.common.config.Global;
import com.civet.myadmin.common.utils.StringUtils;
import com.civet.myadmin.common.web.BaseController;
import com.civet.myadmin.modules.cms.entity.Article;
import com.civet.myadmin.modules.cms.entity.Category;
import com.civet.myadmin.modules.cms.entity.Site;
import com.civet.myadmin.modules.cms.service.CategoryService;
import com.civet.myadmin.modules.cms.service.FileTplService;
import com.civet.myadmin.modules.cms.service.SiteService;
import com.civet.myadmin.modules.cms.utils.TplUtils;
import com.civet.myadmin.modules.enviroment.entity.EnvDispullotion;
import com.google.common.collect.Lists;
import org.apache.shiro.authz.annotation.RequiresUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 栏目Controller
 * @author PlamCivet
 * @version 2013-4-21
 */
@Controller("caseController")
@RequestMapping(value = "${adminPath}/enviroment/case")
public class CaseController extends BaseController {

	@Autowired
	private CategoryService categoryService;
    @Autowired
   	private FileTplService fileTplService;
    @Autowired
   	private SiteService siteService;
	

	@RequestMapping(value = {"list", ""})
	public String list(Model model) {
		List<EnvDispullotion> list = Lists.newArrayList();
		fakerList(list);
		model.addAttribute("list", list);
		return "modules/enviroment/caseList";
	}

	public static void fakerList(List<EnvDispullotion> list) {
		for(int i=0;i<10;i++){
			EnvDispullotion envDispullotion = getEnvDispullotion(i);
			list.add(envDispullotion);
		}
	}

	@RequestMapping(value = "form")
	public String form(Category category, Model model,String id) {

		EnvDispullotion envDispullotion = new EnvDispullotion();
		if(id!=null&!"".equals(id)) {
			envDispullotion = getEnvDispullotion(Integer.parseInt(id));
		}

        model.addAttribute("listViewList",getTplContent(Category.DEFAULT_TEMPLATE));
        model.addAttribute("case_DEFAULT_TEMPLATE",Category.DEFAULT_TEMPLATE);
        model.addAttribute("contentViewList",getTplContent(Article.DEFAULT_TEMPLATE));
        model.addAttribute("article_DEFAULT_TEMPLATE",Article.DEFAULT_TEMPLATE);
		model.addAttribute("case", envDispullotion);
		return "modules/enviroment/caseForm";
	}

	public static EnvDispullotion getEnvDispullotion(int id) {
		EnvDispullotion envDispullotion = new EnvDispullotion();
		envDispullotion.setId(id);
		envDispullotion.setName("测试");
		envDispullotion.setOverPollution("有毒金属化合物10公斤");
		envDispullotion.setPollutionCharge("200万");
		envDispullotion.setPollutionOwe("60万");
		envDispullotion.setAdministrativePenalty("罚款120万以及没收部分超标不合格机器");

		Map energyAndYU = new HashMap();
		energyAndYU.put("water","1000升/年");
		energyAndYU.put("electricity","20万伏特/年");
		energyAndYU.put("fuel","300升/年");
		envDispullotion.setEnergyAndYU(energyAndYU);

		Map materialsAndYU = new HashMap();
		materialsAndYU.put("盐酸","100升/年");
		materialsAndYU.put("黄磷","20公斤/年");
		materialsAndYU.put("煤油","3吨/年");
		envDispullotion.setMaterialsAndYU(materialsAndYU);

		Map productionAndSize = new HashMap();
		productionAndSize.put("电","100万伏特/年");
		productionAndSize.put("燃料","20吨/年");
		productionAndSize.put("电解质水","300吨/年");
		envDispullotion.setProductionAndSize(productionAndSize);

		envDispullotion.setPollutionCategory("有毒金属化合物6斤/年,一氧化碳1升/年");
		envDispullotion.setProcessAndNode("电解，燃烧，氧化");
		envDispullotion.setTechnologyAndFacilities("良好");
		return envDispullotion;
	}

	@RequestMapping(value = "save")
	public String save(Category category, Model model, RedirectAttributes redirectAttributes) {
		if(Global.isDemoMode()){
			addMessage(redirectAttributes, "演示模式，不允许操作！");
			return "redirect:" + adminPath + "/enviroment/case/";
		}
		if (!beanValidator(model, category)){
			return form(category, model,"");
		}
		categoryService.save(category);
		addMessage(redirectAttributes, "保存栏目'" + category.getName() + "'成功");
		return "redirect:" + adminPath + "/enviroment/case/";
	}
	
	@RequestMapping(value = "delete")
	public String delete(Category category, RedirectAttributes redirectAttributes) {
		if(Global.isDemoMode()){
			addMessage(redirectAttributes, "演示模式，不允许操作！");
			return "redirect:" + adminPath + "/enviroment/case/";
		}
		if (Category.isRoot(category.getId())){
			addMessage(redirectAttributes, "删除栏目失败, 不允许删除顶级栏目或编号为空");
		}else{
			categoryService.delete(category);
			addMessage(redirectAttributes, "删除栏目成功");
		}
		return "redirect:" + adminPath + "/enviroment/case/";
	}

	/**
	 * 批量修改栏目排序
	 */
	@RequestMapping(value = "updateSort")
	public String updateSort(String[] ids, Integer[] sorts, RedirectAttributes redirectAttributes) {
    	int len = ids.length;
    	Category[] entitys = new Category[len];
    	for (int i = 0; i < len; i++) {
    		entitys[i] = categoryService.get(StringUtils.toInteger(ids[i]));
    		entitys[i].setSort(sorts[i]);
    		categoryService.save(entitys[i]);
    	}
    	addMessage(redirectAttributes, "保存栏目排序成功!");
		return "redirect:" + adminPath + "/enviroment/case/";
	}
	
	@RequiresUser
	@ResponseBody
	@RequestMapping(value = "treeData")
	public List<Map<String, Object>> treeData(String module, @RequestParam(required=false) String extId, HttpServletResponse response) {
		response.setContentType("application/json; charset=UTF-8");
		List<Map<String, Object>> mapList = Lists.newArrayList();
		return mapList;
	}

    private List<String> getTplContent(String prefix) {
   		List<String> tplList = fileTplService.getNameListByPrefix(siteService.get(Site.getCurrentSiteId()).getSolutionPath());
   		tplList = TplUtils.tplTrim(tplList, prefix, "");
   		return tplList;
   	}
}
